﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EBA.IoC
{
    /// <summary>
    /// 导出约定集合
    /// </summary>
    public class ExportConventions : IEnumerable<ExportConvention>
    {

         Dictionary<Type, TypeExportConventions> Items = new Dictionary<Type, TypeExportConventions>();

        /// <summary>
        /// 索引器
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public IEnumerable<ExportConvention> this[Type type]
        {
            get
            {
               

                TypeExportConventions conventions = null;
               
                if (this.Items.TryGetValue(type, out conventions))
                {
                    return conventions;
                }
                else
                {
                    return Enumerable.Empty<ExportConvention>();
                }

            
            }
        }

        /// <summary>
        /// 新增一个导出约定
        /// </summary>
        /// <param name="contract"></param>
        public void Add(ExportConvention contract)
        {
            TypeExportConventions contracts = null;

            if (this.Items.ContainsKey(contract.ContractType))
            {
                contracts = this.Items[contract.ContractType];
            }
            else
            {
                contracts = new TypeExportConventions();
                this.Items.Add(contract.ContractType, contracts);
            }

            contracts.Add(contract);
        }

        /// <summary>
        /// 获取导出约定迭代器
        /// </summary>
        /// <returns></returns>
        public IEnumerator<ExportConvention> GetEnumerator()
        {
            return this.Items.SelectMany(i => i.Value).GetEnumerator();
        }

        /// <summary>
        /// 获取导出约定迭代器
        /// </summary>
        /// <returns></returns>
        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this.Items.SelectMany(i => i.Value).GetEnumerator();
        }

    }
}
